package com.phpxiu.app.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.UserAccountProductContent;
import com.phpxiu.app.view.UserAccount;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by WuJinZhou on 2016/6/5.
 */
public class UserAccountProductListAdapter extends BaseAdapter<UserAccountProductContent> implements View.OnClickListener {
    private WeakReference<UserAccount>accountWeak;
    private boolean isUSD=false;
    public UserAccountProductListAdapter(UserAccount account, List<UserAccountProductContent> data) {
        super(account, data);
        accountWeak=new WeakReference<UserAccount>(account);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UserAccountProductContent content=data.get(position);
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.user_account_product_list_item_view,null);
        }
        TextView valueView=(TextView)convertView.findViewById(R.id.value_view);
        valueView.setText(content.getCoin());
        TextView extraView=(TextView)convertView.findViewById(R.id.extra_msg_view);
        extraView.setText(content.getTips());
        Button buy=(Button)convertView.findViewById(R.id.buy_btn);
        if(isUSD){
            buy.setText("$"+content.getMoney());
        }else{
            buy.setText("￥"+content.getMoney());
        }
        buy.setTag(content);
        buy.setOnClickListener(this);
        convertView.findViewById(R.id.divider).setVisibility(View.VISIBLE);
        if(data.size()>0&&position==data.size()-1){
            convertView.findViewById(R.id.divider).setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    public boolean isUSD() {
        return isUSD;
    }

    public void setUSD(boolean isUSD) {
        this.isUSD = isUSD;
    }

    @Override
    public void onClick(View v) {
        if(v.getTag()!=null&&v.getTag() instanceof UserAccountProductContent){
            if(accountWeak!=null&&accountWeak.get()!=null){
                accountWeak.get().onOrder((UserAccountProductContent)v.getTag());
            }
        }
    }
}
